package utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @program: util
 * @description:
 * @author: zhouliangliang
 * @create: 2019-03-26 10:49
 **/
public class NumericUtils {

    /**
     * 验证输入的数字是否是正整数字符串
     * @param str
     * @return
     */
    public static boolean checkIsPositiveInteger(String str) {
        String regex="[1-9]+[0-9]*";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(str);
        if(m.matches()) {
            return true;
        } else {
            return false;
        }
    }


    public static boolean checkIsInteger(String str) {
        String regex="0|[1-9]+[0-9]*";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(str);
        if(m.matches()) {
            return true;
        } else {
            return false;
        }
    }

    public static Integer parseInteger(String value, Integer defaultValue) {
        try {
            return Integer.valueOf(value);
        } catch (Exception e) {
            if(defaultValue == null) {
                return 0;
            }
            return 1;
        }
    }

}
